/*==================*\
# Cubes - the 2x2x2  #
# Direct connection  #
# Version 0.x  07.27 #
\*==================*/

// Map a LED# to actual pin#
// ordered so it scans left to right, back to front, top to bottom
const int P[] = { A2, 2, 9, 8, A1, 3, 10, 7 } ; //POC kit
const int Plen=(sizeof(P)/sizeof(P[0])) ; // Number of LEDS

// To save on complicated code - lookup table for each "face's" position in P[]
const int Pxl[] = { 0, 2, 4, 6 } ; // X left
const int Pyf[] = { 2, 3, 6, 7 } ; // Y front
const int Pzt[] = { 0, 1, 2, 3 } ; // Z top
const int Pslen=(sizeof(Pzt)/sizeof(Pzt[0])) ; // number of LEDs in a sides (assuming same for all!)
//(Arduino note: All lookup tables could be placed in F-memory to save RAM)

void setup() {
  // Set pins for simple output
  for ( int n=0; n<Plen; n++ )
    pinMode(P[n],OUTPUT);
  pinMode(13,OUTPUT);      //Debug   startloop marker
}

// Here patterns are generated directly in the loop with code
// The loop only runs once for every complete sequence
// delay() is used as there is no other activity.

void loop() {

digitalWrite(13,HIGH);     //Debug   startloop marker

  // pattern 1 - turn on each LED singly
  for ( int n=0; n<Plen; n++ ) {
    digitalWrite(P[n],HIGH);
    delay(350);
    digitalWrite(P[n],LOW);
    delay(150);
  }
digitalWrite(13,LOW);      //Debug   startloop marker

  // Pattern 2 - turn all LEDs On/Off together, 3 times
  for ( int b=0; b<3; b++ ) {
    for ( int n=0 ; n<Plen ; n++ ) 
      digitalWrite(P[n],HIGH) ;
    delay(350);
    for ( int n=0; n<Plen; n++ ) 
      digitalWrite(P[n],LOW) ;
    delay(150);
  }

  // Pattern 3 - Pan across axis
  // The pattern is defined in the arrays, point to correct array for each axis
    SideSwap(Pxl); // X left/right
    SideSwap(Pyf); // Y front back
    SideSwap(Pzt); // Z top/bottom

  // Pattern 4  - "Replay"
  // A stream of pattern instructions
  // [TBW]

}

void SideSwap(const int On[Pslen]) {
	// light the LEDs in array, and invert LEDs
    for ( int n=0; n<Pslen; n++)     // switch LEDs ON in array
      digitalWrite(P[On[n]],HIGH) ;
    for ( int b=0; b<6; b++ ) {      // repeat the two&fro 3 times
      delay(250);    
      // as it is just two sides that swap, we simply invert all LEDS
      for ( int n=0; n<Plen; n++ )
        digitalWrite(P[n],digitalRead(P[n])==HIGH?LOW:HIGH) ;
    }
    for ( int n=0; n<Pslen; n++)     // Turn all LEDs off (whether on or not)
      digitalWrite(P[On[n]],LOW) ;
}
